package fragmentson;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.model.animation.Animation.AnimationListener;
import com.amap.api.maps.model.animation.TranslateAnimation;
import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import activity.SonActivity;
import bean.EntityForSimple;
import bean.MessageForSimple;
import bean.MessageForSimpleList;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.DateUtils;
import util.dialog.DialogMyDatePicker;

import static com.wq.cycling.R.id.marker;


/**
 * 设备轨迹
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class Track extends BackHandledFragment {

	private View rootView;
	private TextureMapView mapView;
	private AMap aMap;
	//mode 0基础地图1卫星地图 road 0路况模式 1非路况模式
	private int mode=0,road=0;
	private View ll_calendar;
	private int high = 0;
	private TextView tv_time1,tv_time2,tv_time3,tv_time4,tv_time5;
	//慢动作
	//private SmoothMoveMarker smoothMarker;
	private SeekBar timeline;
	private int i=0;
	private ImageView iv_play;
	private int ptptime=1;
	private TextView tv_speed;
	private TextView tv_starttime,tv_endtime;
	private View ll_endtime,ll_starttime;
	private String starttime,endtime;
	//轨迹播放
	private List<LatLng> tracks= null;
	private ArrayList<EntityForSimple> mDatas = new ArrayList<>();
	private Marker markerCar = null,infoMarker = null;
	private Polyline polyline;//线路
	private int index=0;
	private boolean istop = false;
	private PowerManager.WakeLock mWakeLock;
	private long exitTime = 0;
	private Handler handler = new Handler(){

		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			super.dispatchMessage(msg);
			switch (msg.what) {
				case 100:
					moverCar();
					break;
				case 101:
					try {
						index = 0;
						timeline.setProgress(0);
						iv_play.setImageResource(R.mipmap.icon_track_play);
						iv_play.setTag(true);
						istop = true;
						markerCar.setPosition(tracks.get(index));//移动车辆
						aMap.moveCamera(CameraUpdateFactory.changeLatLng(tracks.get(index)));
						//markerCar.remove();
						View view = getActivity().getLayoutInflater().inflate(R.layout.map_custom_info_window, null);
						//((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime()+'\n'+"杭州市西湖区文三路525号卫星大厦8B");
						((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime());
						((ImageView) view.findViewById(marker)).setImageResource(R.mipmap.icon_unline);
						infoMarker.setIcon(BitmapDescriptorFactory.fromView(view));
						infoMarker.setPosition(tracks.get(index));
					}catch (Exception e){

					}
					break;
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_track, null);
		/*初始化地图和amap对象*/
		mapView = (TextureMapView) rootView.findViewById(R.id.map);
		mapView.onCreate(savedInstanceState);// 此方法必须重写
		if (aMap == null) {
			aMap = mapView.getMap();
		}
		initView();
		initMapView();
		initDeviceData();
		return rootView;
	}

	private void initDeviceData(){
		ApiUtil.getApiService().getDevice(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						//如果想改变地图中心点
						aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(Double.valueOf(entity.getData().getLat()), Double.valueOf(entity.getData().getLng())),16,0,0)));
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),"服务器发生错误",Toast.LENGTH_LONG).show();
			}
		});
	}

	public void initData(String starttime,String endtime){
		ApiUtil.getApiService().getTracks(DemoApplication.getToken(),DemoApplication.getDeviceid(),starttime,endtime).enqueue(new Callback<MessageForSimpleList>() {
			@Override
			public void onResponse(Call<MessageForSimpleList> call, Response<MessageForSimpleList> response) {
				try {
					MessageForSimpleList entity = response.body();
					if(entity.getCode().equals("10008")){
						tracks = new ArrayList<LatLng>();
						mDatas = new ArrayList<EntityForSimple>();
						mDatas = entity.getData();
						for(EntityForSimple mData:entity.getData()){
							tracks.add(new LatLng(Double.valueOf(mData.getLat()), Double.valueOf(mData.getLng())));
							/*tracks.add(new LatLng(30.332066, 120.104309));
							tracks.add(new LatLng(30.333548, 120.107142));
							tracks.add(new LatLng(30.333500, 120.123142));
							tracks.add(new LatLng(30.333248, 120.107142));
							tracks.add(new LatLng(30.333500, 120.113142));
							tracks.add(new LatLng(30.333148, 120.106142));*/
						}
						drawline();
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<MessageForSimpleList> call, Throwable t) {
				Toast.makeText(getActivity(),"服务器发生错误",Toast.LENGTH_LONG).show();
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyTag");

		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.map_track);
		rootView.findViewById(R.id.iv_mapmode).setOnClickListener(listener);
		rootView.findViewById(R.id.ll_road).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_plus).setOnClickListener(listener);
		rootView.findViewById(R.id.iv_reduce).setOnClickListener(listener);
		rootView.findViewById(R.id.tv_sure).setOnClickListener(listener);
		rootView.findViewById(R.id.tv_cancel).setOnClickListener(listener);
		ll_calendar = rootView.findViewById(R.id.ll_calendar);

		tv_time1 = ((TextView) rootView.findViewById(R.id.tv_time1));
		tv_time2 = ((TextView) rootView.findViewById(R.id.tv_time2));
		tv_time3 = ((TextView) rootView.findViewById(R.id.tv_time3));
		tv_time4 = ((TextView) rootView.findViewById(R.id.tv_time4));
		tv_time5 = ((TextView) rootView.findViewById(R.id.tv_time5));
		tv_time1.setOnClickListener(timeListener);
		tv_time2.setOnClickListener(timeListener);
		tv_time3.setOnClickListener(timeListener);
		tv_time4.setOnClickListener(timeListener);
		tv_time5.setOnClickListener(timeListener);

		ll_starttime = rootView.findViewById(R.id.ll_starttime);
		ll_starttime.setOnClickListener(listener);
		ll_endtime = rootView.findViewById(R.id.ll_endtime);
		ll_endtime.setOnClickListener(listener);
		tv_starttime = ((TextView) rootView.findViewById(R.id.tv_starttime));
		tv_endtime = ((TextView) rootView.findViewById(R.id.tv_endtime));

		iv_play = ((ImageView) rootView.findViewById(R.id.iv_play));
		iv_play.setOnClickListener(listener);
		iv_play.setTag(true);

		tv_speed = ((TextView) rootView.findViewById(R.id.tv_speed));
		tv_speed.setOnClickListener(listener);
		tv_speed.setTag(true);

		timeline = ((SeekBar) rootView.findViewById(R.id.timeline));
		//timeline.setEnabled(false);
		timeline.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if(fromUser){
					try {
						index = tracks.size()*progress/100;
						if(index==tracks.size()) {
							index = tracks.size()-1;
						}
						Log.d("currenttimell",(System.currentTimeMillis()-exitTime)+"");
						if(System.currentTimeMillis()-exitTime>40) {
							markerCar.setPosition(tracks.get(index));
							View view = getActivity().getLayoutInflater().inflate(R.layout.map_custom_info_window, null);
							//((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime() + '\n' + "杭州市西湖区文三路525号卫星大厦8B");
							((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime());
							((ImageView) view.findViewById(marker)).setImageResource(R.mipmap.icon_unline);
							infoMarker.setIcon(BitmapDescriptorFactory.fromView(view));
							infoMarker.setPosition(tracks.get(index));
							aMap.moveCamera(CameraUpdateFactory.changeLatLng(tracks.get(index)));
						}
						exitTime = System.currentTimeMillis();
					}catch (Exception e){

					}
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				iv_play.setImageResource(R.mipmap.icon_track_play);
				iv_play.setTag(true);
				istop = true;
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				markerCar.setPosition(tracks.get(index));
				View view = getActivity().getLayoutInflater().inflate(R.layout.map_custom_info_window, null);
				//((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime() + '\n' + "杭州市西湖区文三路525号卫星大厦8B");
				((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index).getReporttime());
				((ImageView) view.findViewById(marker)).setImageResource(R.mipmap.icon_unline);
				infoMarker.setIcon(BitmapDescriptorFactory.fromView(view));
				infoMarker.setPosition(tracks.get(index));
				aMap.moveCamera(CameraUpdateFactory.changeLatLng(tracks.get(index)));
			}
		});


		try {
			int widthSpec =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
			int heightSpec =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
			ll_calendar.measure(widthSpec,heightSpec);
			high = ll_calendar.getMeasuredHeight();
		}catch (Exception e){
			high = 400;
		}

		Log.d("dateutle", DateUtils.getDayBegin()+","+DateUtils.getDayEnd()+"||"+DateUtils.getBeginDayOfYesterday()+","+DateUtils.getEndDayOfYesterDay()+"||"+DateUtils.getWeekStartTime()+","+DateUtils.getWeekEndTime()+"||"+DateUtils.getBeforeWeekStartTime()+","+DateUtils.getBeforeWeekEndTime());
	}

	private void initMapView(){
		//隐藏缩放组件
		aMap.getUiSettings().setZoomControlsEnabled(false);
		//logo位置
		aMap.getUiSettings().setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);
		//如果想改变地图中心点
		//加载定位maker
		//定位蓝点
		/*locationBlue();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				locationBlue();
			}
		}, 2000);*/
	}
	public void drawline(){
		aMap.clear();
		aMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(tracks.get(0),15,0,0)));
		if(polyline!=null){
			polyline=null;
		}
		if(tracks!=null){
			if(tracks.size()==1){
				MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
						.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.icon_marker_point)))
						.position(tracks.get(0))
						.anchor(0.5f, 0.5f)
						.draggable(true);
				aMap.addMarker(markerOption);
			}
			if(tracks.size()>1){
				MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
						.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.icon_marker_point)))
						.position(tracks.get(0))
						.anchor(0.5f, 0.5f)
						.draggable(true);
				aMap.addMarker(markerOption);
				MarkerOptions markerOption1 = new MarkerOptions().icon(BitmapDescriptorFactory
						.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.icon_marker_point)))
						.position(tracks.get(tracks.size()-1))
						.anchor(0.5f, 0.5f)
						.draggable(true);
				aMap.addMarker(markerOption1);
			}
		}
		polyline = aMap.addPolyline(new PolylineOptions().addAll(tracks).width(10).color(Color.argb(255, 0, 139, 250)));
		if(markerCar!=null){
			markerCar.destroy();
			markerCar =null;
		}
		//创建移动车辆
		MarkerOptions markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
				.fromBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.track_car)))
				.position(tracks.get(0))
				.anchor(0.5f, 0.5f)
				//.title(mDatas.get(0).getReporttime())
				.draggable(true);
		markerCar = aMap.addMarker(markerOption);
		//markerCar.showInfoWindow();

		//仿infowindow
		View view = getActivity().getLayoutInflater().inflate(R.layout.map_custom_info_window, null);
		//((TextView) view.findViewById(R.id.title)).setText(mDatas.get(0).getReporttime()+'\n'+"杭州市西湖区文三路525号卫星大厦8B");
		((TextView) view.findViewById(R.id.title)).setText(mDatas.get(0).getReporttime());
		((ImageView) view.findViewById(marker)).setImageResource(R.mipmap.icon_unline);
		MarkerOptions marker = new MarkerOptions()
				.icon(BitmapDescriptorFactory.fromView(view))
				.position(tracks.get(0))
				.draggable(true);
		infoMarker = aMap.addMarker(marker);
	}

	public void startPalyback(){
		index=0;
		//创建轨迹线路
		index ++;
		handler.postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				handler.sendEmptyMessage(100);
			}
		}, 100);
	}

	private void moverCar(){
		try {
			Log.d("index000000", index + "");
			markerCar.setRotateAngle((float) getAngle(tracks.get(index - 1), tracks.get(index)));
			//markerCar.setTitle(mDatas.get(index-1).getReporttime());

			com.amap.api.maps.model.animation.Animation animation = new TranslateAnimation(tracks.get(index));
			float distance = AMapUtils.calculateLineDistance(tracks.get(index - 1), tracks.get(index));
			animation.setDuration(((int) (5 * distance) / ptptime));
			animation.setInterpolator(new LinearInterpolator());
			animation.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd() {
					// TODO Auto-generated method stub

					if (index < tracks.size()) {
						timeline.setProgress((index * 100) / tracks.size());
						if (!istop) {
							moverCar();
						}
					} else {
						//重新开始
						handler.sendEmptyMessage(101);
					}
				}
			});

			View view = getActivity().getLayoutInflater().inflate(R.layout.map_custom_info_window, null);
			//((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index-1).getReporttime()+'\n'+"杭州市西湖区文三路525号卫星大厦8B");
			((TextView) view.findViewById(R.id.title)).setText(mDatas.get(index-1).getReporttime());
			((ImageView) view.findViewById(marker)).setImageResource(R.mipmap.icon_unline);
			infoMarker.setIcon(BitmapDescriptorFactory.fromView(view));

			com.amap.api.maps.model.animation.Animation animation1 = new TranslateAnimation(tracks.get(index));
			animation1.setDuration(((int) (5 * distance) / ptptime));
			animation1.setInterpolator(new LinearInterpolator());
			animation1.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd() {
					// TODO Auto-generated method stub

					if (index < tracks.size()) {
						timeline.setProgress((index * 100) / tracks.size());
						if (!istop) {
							moverCar();
						}
					} else {
						//重新开始
						handler.sendEmptyMessage(101);
					}
				}
			});

			markerCar.setAnimation(animation);
			infoMarker.setAnimation(animation1);
			//markerCar.setPosition(tracks.get(index));//移动车辆
			//polyline.setPoints(tracks.subList(0, index));//显示轨迹
			aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(tracks.get(index), aMap.getCameraPosition().zoom), 1000, null);
			markerCar.startAnimation();
			infoMarker.startAnimation();
			index++;
		} catch (Exception e) {
		}
	}


	public void locationBlue(){
		MyLocationStyle myLocationStyle;
		myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
		myLocationStyle.strokeColor(Color.argb(0,0,0,0));
		myLocationStyle.radiusFillColor(Color.argb(0,0,0,0));//设置定位蓝点精度圆圈的填充颜色的方法。
		myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.map_arrows));// 设置小蓝点的图标
		myLocationStyle.anchor(0.5f,0.8f);
		myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//指定为一次并拉到中心
		//myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);//指定为一次并拉到中心
		myLocationStyle.interval(10000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
		aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
		aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
	}

	public static Track newInstance() {
		Track fragment = new Track();
		return fragment;
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()){
				case R.id.iv_mapmode:
					if(mode==0) {
						((ImageView) v).setImageResource(R.mipmap.icon_right_map3);
						aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
						mode = 1;
					}else{
						((ImageView) v).setImageResource(R.mipmap.icon_right_map2);
						aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
						mode =0;
					}
					break;
				case R.id.ll_road:
					if(road==0) {
						((ImageView) rootView.findViewById(R.id.iv_road)).setImageResource(R.mipmap.icon_right_map5);
						aMap.setTrafficEnabled(true);
						road = 1;
					}else{
						((ImageView) rootView.findViewById(R.id.iv_road)).setImageResource(R.mipmap.icon_right_map4);
						aMap.setTrafficEnabled(false);
						road = 0;
					}
					break;
				case R.id.iv_plus:
					aMap.moveCamera(CameraUpdateFactory.zoomIn());
					break;
				case R.id.iv_reduce:
					aMap.moveCamera(CameraUpdateFactory.zoomOut());
					break;
				case R.id.tv_sure:
					initData(starttime.replace("-","").replace(" ","").replace(":","")+"00",endtime.replace("-","").replace(" ","").replace(":","")+"00");
					HideOrderList();
					break;
				case R.id.tv_cancel:
					HideOrderList();
					break;
				case R.id.iv_play:
					if(tracks!=null&&tracks.size()>1) {
						if ((Boolean) iv_play.getTag()) {
							iv_play.setImageResource(R.mipmap.icon_track_stop);
							iv_play.setTag(false);
							istop = false;
							if (index == 0) {
								startPalyback();
							} else if (index > 0) {
								moverCar();
							}
						} else {
							iv_play.setImageResource(R.mipmap.icon_track_play);
							iv_play.setTag(true);
							istop = true;
						}
					}
					break;
				case R.id.tv_speed:
					if (((Boolean) tv_speed.getTag())) {
						tv_speed.setText("快");
						tv_speed.setTag(false);
						ptptime = 2;
					} else {
						tv_speed.setText("慢");
						tv_speed.setTag(true);
						ptptime = 1;
					}
					break;
				case R.id.ll_starttime:
					DialogMyDatePicker startdialog = new DialogMyDatePicker(getActivity());
					startdialog.setSureListener(new DialogMyDatePicker.Sure() {
						@Override
						public void onSure(String item) {
							starttime = item;
							tv_starttime.setText(starttime);
						}
					});
					startdialog.show();
					break;
				case R.id.ll_endtime:
					DialogMyDatePicker enddialog = new DialogMyDatePicker(getActivity());
					enddialog.setSureListener(new DialogMyDatePicker.Sure() {
						@Override
						public void onSure(String item) {
							endtime = item;
							tv_endtime.setText(endtime);
						}
					});
					enddialog.show();
					break;
				default:
					break;
			}
		}
	};

	private View.OnClickListener timeListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			tv_time1.setBackgroundResource(R.drawable.corner_view_white3);
			tv_time1.setTextColor(getResources().getColor(R.color.textcolor3));
			tv_time2.setBackgroundResource(R.drawable.corner_view_white3);
			tv_time2.setTextColor(getResources().getColor(R.color.textcolor3));
			tv_time3.setBackgroundResource(R.drawable.corner_view_white3);
			tv_time3.setTextColor(getResources().getColor(R.color.textcolor3));
			tv_time4.setBackgroundResource(R.drawable.corner_view_white3);
			tv_time4.setTextColor(getResources().getColor(R.color.textcolor3));
			tv_time5.setBackgroundResource(R.drawable.corner_view_white3);
			tv_time5.setTextColor(getResources().getColor(R.color.textcolor3));
			switch (v.getId()){
				case R.id.tv_time1:
					handler.sendEmptyMessage(101);
					aMap.clear();
					tv_time1.setBackgroundResource(R.drawable.corner_view_green_white);
					tv_time1.setTextColor(getResources().getColor(R.color.iscur));
					initData(DateUtils.getDayBegin(),DateUtils.getDayEnd());
					HideOrderList();
					break;
				case R.id.tv_time2:
					handler.sendEmptyMessage(101);
					aMap.clear();
					tv_time2.setBackgroundResource(R.drawable.corner_view_green_white);
					tv_time2.setTextColor(getResources().getColor(R.color.iscur));
					initData(DateUtils.getBeginDayOfYesterday(),DateUtils.getEndDayOfYesterDay());
					HideOrderList();
					break;
				case R.id.tv_time3:
					handler.sendEmptyMessage(101);
					aMap.clear();
					tv_time3.setBackgroundResource(R.drawable.corner_view_green_white);
					tv_time3.setTextColor(getResources().getColor(R.color.iscur));
					initData(DateUtils.getWeekStartTime(),DateUtils.getWeekEndTime());
					HideOrderList();
					break;
				case R.id.tv_time4:
					handler.sendEmptyMessage(101);
					aMap.clear();
					tv_time4.setBackgroundResource(R.drawable.corner_view_green_white);
					tv_time4.setTextColor(getResources().getColor(R.color.iscur));
					initData(DateUtils.getBeforeWeekStartTime(),DateUtils.getBeforeWeekEndTime());
					HideOrderList();
					break;
				case R.id.tv_time5:
					handler.sendEmptyMessage(101);
					aMap.clear();
					tv_time5.setBackgroundResource(R.drawable.corner_view_green_white);
					tv_time5.setTextColor(getResources().getColor(R.color.iscur));

					String month="",day="",hour="",minute="";
					if(starttime==null||endtime==null){
						Calendar c = Calendar.getInstance();
						int mYear = c.get(Calendar.YEAR);
						int mMonth = c.get(Calendar.MONTH);
						int mDay = c.get(Calendar.DAY_OF_MONTH);
						int mHour = c.get(Calendar.HOUR_OF_DAY);
						int mMinute = c.get(Calendar.MINUTE);

						if(mMonth+1<10){
							month = "0"+(mMonth+1);
						}else{
							month = (mMonth+1)+"";
						}
						if(mDay<10){
							day = "0"+mDay;
						}else {
							day = mDay+"";
						}
						if(mHour<10){
							hour = "0"+mHour;
						}else {
							hour = mHour+"";
						}
						if(mMinute<10){
							minute = "0"+mMinute;
						}else {
							minute = mMinute+"";
						}
						endtime = starttime = mYear+"-"+month+"-"+day+" "+hour+":"+minute;
					}
					tv_starttime.setText(starttime);
					tv_endtime.setText(endtime);
					ShowOrderList();
					break;
				default:
					break;
			}
		}
	};


	private void ShowOrderList() {
		if(ll_calendar.getVisibility()==View.GONE){
			mapView.onResume();
			ll_calendar.setVisibility(View.VISIBLE);
			android.view.animation.TranslateAnimation anim = new android.view.animation.TranslateAnimation(0, 0, -high, 0);
			anim.setDuration(200);
			ll_calendar.startAnimation(anim);
		}

	}

	private void HideOrderList() {
		if(ll_calendar.getVisibility()==View.VISIBLE){
			ll_calendar.setVisibility(View.GONE);
			android.view.animation.TranslateAnimation anim = new android.view.animation.TranslateAnimation(0, 0, 0, -high);
			anim.setDuration(300);
			ll_calendar.startAnimation(anim);
		}
	}

	@Override
	public boolean onBackPressed() {
		((SonActivity) getActivity()).finish();
		return true;
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onResume() {
		super.onResume();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onPause() {
		super.onPause();
		mapView.onPause();
		try {
			mWakeLock.release();
		}catch (Exception e){

		}

	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}


	/**
	 * 根据两点算取图标转的角度
	 */
	private double getAngle(LatLng fromPoint, LatLng toPoint) {
		double slope = getSlope(fromPoint, toPoint);
		if (slope == Double.MAX_VALUE) {
			if (toPoint.latitude > fromPoint.latitude) {
				return 0;
			} else {
				return 180;
			}
		}
		float deltAngle = 0;
		if ((toPoint.latitude - fromPoint.latitude) * slope < 0) {
			deltAngle = 180;
		}
		double radio = Math.atan(slope);
		double angle = 180 * (radio / Math.PI) + deltAngle - 90;
		return angle;
	}

	/**
	 * 算斜率
	 */
	private double getSlope(LatLng fromPoint, LatLng toPoint) {
		if (toPoint.longitude == fromPoint.longitude) {
			return Double.MAX_VALUE;
		}
		double slope = ((toPoint.latitude - fromPoint.latitude) / (toPoint.longitude - fromPoint.longitude));
		return slope;

	}


}
